/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.model;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.siro.pm.utils.DatabaseOperation;
import com.siro.pm.utils.DatabasePool;
import com.siro.pm.utils.PmUtils;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since UserAttendance.java v1.0
 */
public class UserAttendance implements Serializable {
	private static final long serialVersionUID = -8553134708902524041L;
	private static final Logger logger = LoggerFactory
			.getLogger(UserAttendance.class);

	// id,状态,考勤日期，用户名称，描述，出差城市，客户名称
	private String id;
	private String status;
	private String attencedate;
	private String username;
	private String description;
	private String citiname;
	private String bankname;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getAttencedate() {
		return attencedate;
	}

	public void setAttencedate(String attencedate) {
		this.attencedate = attencedate;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getCitiname() {
		return citiname;
	}

	public void setCitiname(String citiname) {
		this.citiname = citiname;
	}

	public String getBankname() {
		return bankname;
	}

	public void setBankname(String bankname) {
		this.bankname = bankname;
	}

	public Attendance toAttendance() {
		Attendance attendance = new Attendance();
		attendance.setId(id);
		attendance.setStatus(status);
		try {
			attendance.setStart(PmUtils.getSDF().parse(attencedate));
			attendance.setEnd(PmUtils.getSDF().parse(attencedate));
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
			return null;
		}
		attendance.setDescription(description);
		attendance.setWhere(citiname);
		attendance.setBankName(bankname);
		return attendance;
	}

	public static void insertUserAttendance(UserAttendance userAttendance) {
		logger.info("添加员工考勤，date:{},userName:{}",
				userAttendance.getAttencedate(), userAttendance.getUsername());
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO siropm_attendance VALUES('");
		sql.append(userAttendance.getId()).append("','");
		sql.append(userAttendance.getStatus()).append("','");
		sql.append(userAttendance.getAttencedate()).append("','");
		sql.append(userAttendance.getUsername()).append("','");
		sql.append(userAttendance.getDescription()).append("','");
		sql.append(userAttendance.getCitiname()).append("','");
		sql.append(userAttendance.getBankname()).append("')");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("添加员工考勤，date:{},userName:{} 成功",
				userAttendance.getAttencedate(), userAttendance.getUsername());
	}

	public static void updateUserAttendance(UserAttendance userAttendance) {
		logger.info("更新员工考勤，date:{},userName:{}",
				userAttendance.getAttencedate(), userAttendance.getUsername());
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE siropm_attendance SET ");
		sql.append("status='").append(userAttendance.getStatus()).append("', ");
		sql.append("attencedate='").append(userAttendance.getAttencedate())
				.append("', ");
		sql.append("username='").append(userAttendance.getUsername())
				.append("',");
		sql.append("description='").append(userAttendance.getDescription())
				.append("',");
		sql.append("citiname='").append(userAttendance.getCitiname())
				.append("',");
		sql.append("bankname='").append(userAttendance.getBankname())
				.append("'");
		sql.append("WHERE id='").append(userAttendance.getId()).append("'");
		try {
			int count = DatabaseOperation.excuteUpdateSQL(
					DatabasePool.getConnection(), sql.toString());
			if (count == 0) {
				insertUserAttendance(userAttendance);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("更新员工考勤，date:{},userName:{} 成功",
				userAttendance.getAttencedate(), userAttendance.getUsername());
	}

	public static List<UserAttendance> getUserAttendanceByDate(
			String startDate, String endDate) {
		logger.info("获取时间段 {}-{}所有考勤数据.", startDate, endDate);
		List<UserAttendance> userAttendances = new ArrayList<UserAttendance>();
		StringBuffer sql = new StringBuffer(
				"SELECT id,status,attencedate,username,description,citiname,bankname FROM siropm_attendance WHERE 1=1 ");
		sql.append(" AND ");
		sql.append("attencedate>='").append(startDate).append("'");
		sql.append(" AND ");
		sql.append("attencedate<='").append(endDate).append("'");
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(),
							sql.toString());
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				UserAttendance userAttendance = new UserAttendance();
				userAttendance.setId(row.get(0));
				userAttendance.setStatus(row.get(1));
				userAttendance.setAttencedate(row.get(2));
				if (row.get(3) != null && !row.get(3).equals(""))
					userAttendance.setUsername(row.get(3));
				if (row.get(4) != null && !row.get(4).equals(""))
					userAttendance.setDescription(row.get(4));
				if (row.get(5) != null && !row.get(5).equals(""))
					userAttendance.setCitiname(row.get(5));
				if (row.get(6) != null && !row.get(6).equals(""))
					userAttendance.setBankname(row.get(6));
				userAttendances.add(userAttendance);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return userAttendances;
	}

	public static List<UserAttendance> getUserAttendanceByDateAndUser(
			String username, String startDate, String endDate) {
		logger.info("获取员工 " + username + " 时间段 {}-{}所有考勤数据.", startDate,
				endDate);
		List<UserAttendance> userAttendances = new ArrayList<UserAttendance>();
		StringBuffer sql = new StringBuffer(
				"SELECT id,status,attencedate,username,description,citiname,bankname FROM siropm_attendance WHERE 1=1 ");
		sql.append(" AND ");
		sql.append("attencedate>='").append(startDate).append("'");
		sql.append(" AND ");
		sql.append("attencedate<='").append(endDate).append("'");
		sql.append(" AND ");
		sql.append("username<='").append(username).append("'");
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(),
							sql.toString());
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				UserAttendance userAttendance = new UserAttendance();
				userAttendance.setId(row.get(0));
				userAttendance.setStatus(row.get(1));
				userAttendance.setAttencedate(row.get(2));
				if (row.get(3) != null && !row.get(3).equals(""))
					userAttendance.setUsername(row.get(3));
				if (row.get(4) != null && !row.get(4).equals(""))
					userAttendance.setDescription(row.get(4));
				if (row.get(5) != null && !row.get(5).equals(""))
					userAttendance.setCitiname(row.get(5));
				if (row.get(6) != null && !row.get(6).equals(""))
					userAttendance.setBankname(row.get(6));
				userAttendances.add(userAttendance);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return userAttendances;
	}
}
